package com.lil.qmui.ui.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lil.qmui.R;
import com.lil.qmui.app.MyApp;
import com.lil.qmui.utils.NetUtil;
import com.lil.qmui.utils.ToastUtils;

import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

/***************************************************************************
 *
 * 生命周期 场景演示 :
 *
 * 切换到该Fragment
 * onAttach-onCreate-onCreateView-onActivityCreated-onStart-onResume
 *
 * 屏幕灭掉 onPause-onSaveInstanceState-onStop
 *
 * 屏幕解锁 onStart-onResume
 *
 * 切换到其他Fragment onPause-onStop-onDestroyView
 *
 * 切换回本身的Fragment onCreateView-onActivityCreated-onStart-onResume
 *
 * 回到桌面 onPause-onSaveInstanceState-onStop
 *
 * 回到应用 onStart-onResume
 *
 * 退出应用 onPause-onStop-onDestroyView-onDestroy-onDetach
 ***************************************************************************/

public abstract class BaseFragment extends Fragment {

    //常量定义，从10000开始，其他界面自己定义的从1000开始
    /**
     * show toast
     */
    public final static int SHOW_TOAST = 10000;

    /**
     * listview item中包含的checkBox 选择状态触发
     */
    public final static int LIST_ITEM_CHECKED = 10001;
    /**
     * 刷新数据
     */
    public final static int REFRESH_LIST = 1001;

    /**
     * 退出登录
     */
    public final static int LOGOUT = 1002;

    /**
     * @param inflater
     * @return 需要显示的view
     */
    public abstract View getView(LayoutInflater inflater, ViewGroup parent);

    /**
     * 程序入库/UI控件注入
     */
    public abstract void init(View view);

    /**
     * 控件响应
     */
    public abstract void data();

    /**
     * Toast输出
     *
     * @param text
     */
    public void showToast(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        ToastUtils.showToast(text);
    }

    public void showToast(Object object) {
        if (object == null) {
            return;
        }
        ToastUtils.showToast(object.toString());
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setRetainInstance(true);// 保存状态
    }

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//		if (view == null) {
        view = getView(inflater, container);
//		}
        // 缓存的view需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        init(view);
        ButterKnife.bind(this, view);
        data();
        return view;
    }

    /**
     * 网络是否连接
     *
     * @return
     */
    public boolean isConnect() {
        if (NetUtil.isNetworkAvailable(MyApp.getContext())) {
            return true;
        }
        showToast(R.string.check_the_network_connection);
        return false;
    }

    public ProgressDialog pDialog;

    public void initProgressDialog(String s, Activity context) {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
            // return;
        }
        pDialog = initProgressDialog(context, ProgressDialog.STYLE_SPINNER, s);
        pDialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface arg0) {
                if (pDialog.isShowing()) {
                    pDialog.dismiss();
                }
            }
        });
        pDialog.show();
    }

    public void initCountTimeProgressDialog(String s, Activity context,
                                            boolean autoSetOnCancelListener) {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
            // return;
        }
        pDialog = initProgressDialog(context, ProgressDialog.STYLE_SPINNER, s,
                true);
        if (autoSetOnCancelListener) {
            pDialog.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    if (pDialog.isShowing()) {
                        pDialog.dismiss();
                    }
                }
            });
        }
        pDialog.show();

    }

    public void setProgressDialogCancelListener(OnCancelListener listener) {
        pDialog.setOnCancelListener(listener);
    }

    /**
     * ProgressDialog 封装
     *
     * @param context
     * @param style
     * @param message
     * @return mProgressDialog
     */
    public static ProgressDialog initProgressDialog(Context context, int style,
                                                    String message) {
        ProgressDialog mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setProgressStyle(style);
        mProgressDialog.setMessage(message);
        mProgressDialog.setCancelable(false);
        return mProgressDialog;
    }

    /**
     * ProgressDialog 封装
     *
     * @param context
     * @param style
     * @param message
     * @return mProgressDialog
     */
    public static ProgressDialog initProgressDialog(Context context, int style,
                                                    String message, boolean cancelable) {
        ProgressDialog mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setProgressStyle(style);
        mProgressDialog.setMessage(message);
        mProgressDialog.setCancelable(cancelable);
        return mProgressDialog;
    }

    public void dismissProgress() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    public float dp2px(int dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }
}
